<template>
	<view class="orderConfirm">
		<navBar title="订单结算"></navBar>
		<view class="address">
			<view class="type" v-if="canSelfDelivery">
				<text>{{canSelfDelivery ? '选择' : ''}}配送方式</text>
				<view class="select-container">
					<view class="radio" v-for="(item, index) in addressType" :key="index"
						@click="changeAddressType(item.type)">
						<view :class="{'check-cirle': true, 'active': form.addressType == item.type}"></view>
						<text>{{item.name}}</text>
					</view>
				</view>
			</view>
			<swiper v-if="form.addressType == 1" style="height: 270rpx;margin-top: 30rpx;" :disable-touch="true">
				<swiper-item v-for="item in shops" :key="item.id">
					<view class="self">
						<view class="hint">
							<u-icon name="map"></u-icon>
							<text>自提地址</text>
						</view>
						<view class="detail">
							{{item.address}}
						</view>
						<view class="hint">
							<text class="distance">当前位置距离门店 {{item.distanceText}}</text>
							<text class="look-map" @click="openMap(item)">查看地图</text>
						</view>
					</view>
				</swiper-item>
			</swiper>
			<view class="express" v-else @click="choseAddress">
				<block v-if="orderAddress">
					<view class="left">
						<view class="address">
							<view class="default" v-if="orderAddress.city_id">默认</view>
							<text>{{orderAddress.city}}</text>
						</view>
						<view class="address-detail">{{orderAddress.city_details}}</view>
						<view class="name">{{orderAddress.name}} {{orderAddress.mobile}}</view>
					</view>
					<u-icon name="arrow-right" color="#999999"></u-icon>
				</block>
				<view v-else class="no-add">
					<u-icon name="plus" class="icon" size="30" color="#FF7744"></u-icon>
					<text>添加收货的地址</text>
				</view>
			</view>
		</view>
		<view class="orderConfirm-bd" v-if="false">
			<view class="title">
				<image class="icon" mode="aspectFit" :src="`${STATIC_URL}/flag.png`" />
				<view class="text">商品信息</view>
			</view>
			<view class="list">
				<template v-for="item in goods">
					<view class="list-item" :key="item.id">
						<image class="cover" mode="aspectFill" :src="item.cover" />
						<view class="info">
							<view class="info-title">
								{{ item.name }}
							</view>
							<!-- <template v-for="size in item.size">
                <view class="info-size" :key="size.id"> {{ size.name }} </view>
              </template> -->
							<view v-if="item.size">{{item.size}}</view>
							<view class="info-other">
								<view class="price"> ¥ {{ item.price }}</view>
								<view class="num"> ×{{ item.num }}</view>
							</view>
						</view>
					</view>
				</template>
			</view>
			<view class="total">
				<view class="total-nums"> 共{{ goods.length }}件 </view>
				<view class="total-text"> 合计： </view>
				<view class="total-price"> ¥ {{ totalPrice }} </view>
			</view>
		</view>
		<!-- <view class="orderConfirm-oper">
			<view class="nums"> 共{{ goods.length }}件 </view>
			<view class="price-group">
				<view class="item">
					<view class="text"> 合计： </view>
					<view class="price"> ¥ {{ totalPrice - gold_num }} </view>
				</view>
				<view class="item">
					<view class="text"> 会员优惠： </view>
					<view class="price" style="color: #CD8544;"> ¥ {{ gold_num }} </view>
				</view>
			</view>
			<view class="btn" @click="buy"> 确认购买 </view>
		</view> -->
		<view class="container products">
			<view class="header">
				<image :src="`${STATIC_URL2}order-icon.png`" mode="widthFix"></image>
				<text>奥莱自营</text>
			</view>
			<view class="list" v-if="goods.length < 2">
				<view class="item" v-for="item in goods" :key="item.id">
					<image class="cover" :src="item.cover" mode="aspectFill"></image>
					<view class="right">
						<view class="title text-hidden">{{item.name}}</view>
						<!-- <block v-for="target in item.size">
							<view class="info-size" :key="size.id"> {{ target.name }} </view>
						</block> -->
						<view v-if="item.size" class="size">{{item.size}}</view>
						<view class="price-num">
							<view class="price">
								<text class="tag">¥</text>
								<text>{{ item.price.split(".")[0] }}</text>
								<text class="tag">.{{ item.price.split(".")[1] }}</text>
							</view>
							<view class="num">
								<u-icon name="minus" @click="minusNum(item)"
									:color="item.num == 1 ? '#D8D8D8' : '#333333'"></u-icon>
								<view class="num-box">{{item.num}}</view>
								<u-icon name="plus" @click="addNum(item)"></u-icon>
							</view>
						</view>
					</view>
				</view>
			</view>
			<view class="list-preview" @click="openList" v-else>
				<view class="left">
					<image class="goods" v-for="(item, index) in goods" v-if="index < 2" :key="item.id"
						:src="item.cover" mode="aspectFill">
					</image>
				</view>
				<view>
					共{{goodsNum}}件
					<u-icon name="arrow-right"></u-icon>
				</view>
			</view>
		</view>
		<view class="container price-detailed">
			<view class="item">
				<text class="label">商品金额</text>
				<view class="right price">
					¥{{allPrice}}
				</view>
			</view>
			<view class="item" v-if="form.addressType === 0">
				<text class="label">运费</text>
				<view class="right price">
					¥{{addInfo.freight}}
				</view>
			</view>
			<view class="item">
				<text class="label">SVIP会员立减</text>
				<view class="right price red">
					- ¥{{vipMinusPrice}}
				</view>
			</view>
			<view class="item" @click="selectCoupon">
				<text class="label">优惠券</text>
				<view class="right">
					<text>{{ couponInfo ? couponInfo.coupon_name : '不可用'}}</text>
					<u-icon name="arrow-right" class="icon" color="#999999"></u-icon>
				</view>
			</view>
			<view class="item" @click='openIntegralModal'>
				<text class="label">积分</text>
				<view class="right">
					<block v-if="useIntegral">
						<text style="white-space: nowrap;"
							v-if="userInfo.gold_num > 0">当前可使用{{gold_num}}积分</text>
						<text v-else>无可用</text>
					</block>
					<text v-else>
						不使用
					</text>
					<u-icon name="arrow-right" class="icon" color="#999999"></u-icon>
				</view>
			</view>
			<view class="item">
				<text class="label">可用余额</text>
				<view class="right price">
					<text>¥{{userInfo.money}}</text>
					<u-icon name="arrow-right" class="icon" color="#999999"></u-icon>
				</view>
			</view>
		</view>
		<!-- 占位 -->
		<view style="height: 230rpx;"></view>
		<view class="bottom">
			<view class="vip-card" v-if="!isVip" @click="openVip">
				<text class="desc">升级SVIP，本单可省</text>
				<view class="save-price">{{vipMinusPrice}}元</view>
				<text>¥{{vipPrice}}/年</text>
				<u-icon name="arrow-right" color="#999999" size="20" class="icon"></u-icon>
			</view>
			<view class="count">
				<view class="price">
					<view class="real">
						<text class="tag">¥</text>
						<text>{{finalPrice}}</text>
						<!-- <text class="tag">.00</text> -->
					</view>
					<view class="save">共省¥{{(allPrice - finalPrice + parseInt(addInfo.freight)).toFixed(2)}}</view>
				</view>
				<view class="submit-button" @click="buy">提交订单</view>
			</view>
		</view>
		<button v-if="showBtn" class="orderConfirm-setting" open-type="openSetting" @opensetting="openSetting">
			去授权
		</button>
		<u-action-sheet :list="payList" v-model="payState" @click="payClick"></u-action-sheet>
		<u-popup v-model="remark" border-radius="15" width="500" height="300" mode="center">
			<view class="popup">
				<text class="header">支付提示</text>
				<text class="title">&nbsp;&nbsp;您账户的金币数量不足以抵扣所有商品优惠，部分商品将按照原价购买，是否确认？</text>
				<view class="btns">
					<view class="btn" style="border-right: none;" @click="()=>remark = false">取消</view>
					<view class="btn" style="color: #006EFF;" @click="surePay">确认</view>
				</view>
			</view>
		</u-popup>
		<u-popup v-model="showList" mode="bottom" border-radius="25">
			<view class="product-list-modal">
				<view class="top">
					<text>商品清单</text>
					<view class="flex-center right">
						<text>共{{goodsNum}}件</text>
						<u-icon name="close" class="close" size="26" color="#999999" @click="showList = false"></u-icon>
					</view>
				</view>
				<scroll-view :scroll-y="true" class="product-container list">
					<view class="item" v-for="item in goods" :key="item.id">
						<image class="cover" :src="item.cover" mode="aspectFill"></image>
						<view class="right">
							<view class="title text-hidden">{{item.name}}</view>
							<view v-if="item.size">{{item.size}}</view>
							<view class="price-num">
								<view class="price">
									<text class="tag">¥</text>
									<text>{{item.price.split(".")[0]}}</text>
									<text class="tag">.{{item.price.split(".")[1]}}</text>
								</view>
								<view class="num">
									<u-icon name="minus" @click="minusNum(item)"
										:color="item.num == 1 ? '#D8D8D8' : '#333333'"></u-icon>
									<view class="num-box">{{item.num}}</view>
									<u-icon name="plus" @click="addNum(item)"></u-icon>
								</view>
							</view>
						</view>
					</view>
				</scroll-view>
			</view>
		</u-popup>
		<u-popup v-model="showIntegralModal" mode="bottom" border-radius="15">
			<view class="integral-modal">
				<view class="item" @click="useIntegral = !useIntegral">
					<u-icon name="checkmark-circle-fill" class="icon" color="#CB712B" v-if="useIntegral"></u-icon>
					<u-icon name="checkmark-circle" class="icon" v-else></u-icon>
					<text>使用积分抵扣</text>
				</view>
				<view class="confirm" @click="showIntegralModal = false">确定</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	import {
		payment
	} from "@/utils/payment";
	import {
		placeOrder,
		vipCityList,
		vipTextDesc,
		vipCityOpen,
		vipStoreOpen,
		vipOpen,
		vipCitySucceed,
		memberUpdateApi,
		address,
	} from "@/utils/api";
	import {
		confirmAddApi,
		integralScaleApi,
		distanceApi
	} from "@/api/index.js"
	import {vipInfoApi} from "@/api/my.js"
	import qqmapsdk from "@/utils/map";
	import {
		mapGetters,
		mapActions
	} from "vuex";
	export default {
		data() {
			return {
				showBtn: false,
				payList: [{
						text: "余额支付",
						value: 0,
					},
					{
						text: "微信支付",
						value: 1,
					},
				],
				payState: false,
				totalPrice: 0,
				province: "",
				city: "",
				district: "",
				vipBuy: false,
				// 金币抵扣数
				gold_num: 0,
				remark: false,
				isUpdate: false,
				addressType: [{
						name: '门店自提',
						type: 1
					},
					{
						name: '快递送货',
						type: 0
					},
				],
				form: {
					addressType: 1
				},
				allProduct: [],
				showList: false,
				couponInfo: "",
				addInfo: {},
				// 是否使用积分
				useIntegral: true,
				showIntegralModal: false,
				proportion: 0,
				// 商店
				shops: [],
				vipPrice: 0
			};
		},
		computed: {
			...mapGetters(["userInfo", "orderAddress", "goods", "vipType", "shareId", 'isVip']),
			//  totalPrice() {
			//    let sum=0;
			//    this.goods.map(item=>{
			//      sum+=Number(item.price)
			//    })
			// sum -= this.gold_num;
			//    return sum;
			//  },
			// 参与计算价格的key
			priceKey() {
				if (this.isVip) {
					return 'reduce_rmb'
				} else {
					return 'price'
				}
			},
			canSelfDelivery() {
				var flag = true
				var len = this.goods.filter(item => item.third_flag == 1).length
				flag = len !== 0
				if (!flag) {
					this.form.addressType = 0
					this.addressType = this.addressType.filter(item => item.type === 0)
				}
				return flag
			},
			vipMinusPrice() {
				var discount = 0
				this.goods.forEach(item => {
					discount += Number(item.price - item.reduce_rmb) * Number(item.num)
				})
				return discount.toFixed(2)
			},
			finalPrice() {
				var price = 0
				this.goods.forEach(item => {
					price += Number(item[this.priceKey]) * Number(item.num)
				})
				if (this.couponInfo) {
					price -= this.couponInfo.coupon_denomination
				}
				// 折扣计算
				if (this.useIntegral) {
					// var integral = (this.userInfo.gold_num / this.proportion)
					this.getGoldNum()
					price -= this.gold_num
				}
				// 增加运费
				if (this.form.addressType === 0) {
					price += parseFloat(this.addInfo.freight || 0)
				}
				return price.toFixed(2)
			},
			goodsNum() {
				var num = 0
				this.goods.forEach(item => {
					num += Number(item.num)
				})
				return num
			},
			allPrice() {
				var price = 0
				this.goods.forEach(item => {
					price += Number(item.price) * Number(item.num)
				})
				return price
			}
		},
		onLoad(opt) {
			this.allProduct = this.goods
			console.log(this.goods);
			this.isUpdate = opt.isUpdate == 1
			// 隐藏余额支付
			// this.payList.shift()
			this.initSettlementInfo()
			// 页面栈
			var pages = getCurrentPages();
			const pre = pages[pages.length - 2]; // pages/goodsDetails/goodsDetails
			const third = pages[pages.length - 3]; //pages/vip/vip
			try {
				if (
					pre.route == "pages/goodsDetails/goodsDetails" &&
					third.route == "pages/vip/vip"
				) {
					this.vipBuy = true;
				}
			} catch (e) {

			}
			this.getVipPrice()
		},
		methods: {
			...mapActions(["setAttr", 'saveGoodsTemporary', 'setOrderAddress']),
			buy() {
				if (!(this.orderAddress && Object.keys(this.orderAddress).length != 0)) {
					uni.showToast({
						icon: "none",
						title: "先选择地址",
					});
				} else {
					this.payState = true;
					// if(this.userInfo.gold_num >= cost){
					// 	this.payState = true;
					// }else{
					// 	this.remark = true;
					// }
				}
			},
			async getVipPrice(){
				let {data} = await vipInfoApi()
				let price = data[0].vip_price || 0
				this.vipPrice = price
			},
			changeAddressType(type) {
				this.form.addressType = type
			},
			openList() {
				this.showList = true;
			},
			addNum(item) {
				item.num++;
			},
			openIntegralModal() {
				this.showIntegralModal = true
			},
			minusNum(item) {
				if (item.num > 1) {
					item.num--;
				}
			},
			openMap(item) {
				uni.openLocation({
					longitude: parseFloat(item.lng),
					latitude: parseFloat(item.lat),
					success: (res) => {
						console.log('打开成功');
					},
					fail: (err) => {
						console.log(err);
					}
				})
			},
			openVip() {
				this.$Router.pushTab('/pages/tabs/memberIntro/memberIntro')
			},
			async initSettlementInfo() {
				if (!this.orderAddress) {
					let {
						data
					} = await address()
					// 暂未设置地址
					if (data.length === 0) return
					// 优先填入默认地址
					var add = data.filter(item => item.city_id)[0] || data[0]
					await this.setOrderAddress(add)
				}
				var {
					data
				} = await confirmAddApi({
					addr_id: this.orderAddress.id
				})
				console.log(data);
				// 邮费
				if (data.is_remote_area) {
					data.freight = data.freight_weight
				}
				this.addInfo = data
				// 积分折扣比例
				var {
					data
				} = await integralScaleApi()
				this.proportion = data.proportion
				// 计算用户地址
				// #ifdef H5
				distanceApi().then(({
					data
				}) => {
					this.shops = data[0]
				})
				// #endif
				// #ifdef MP-WEIXIN
				// 获取定位授权
				uni.authorize({
					scope: 'scope.userLocation',
					success: (res) => {
						uni.getLocation({
							type: 'wgs84',
							success: (res) => {
								distanceApi({
									lng: res.longitude,
									lat: res.latitude
								}).then(({
									data
								}) => {
									this.shops = [data.shift()]
								})
							},
							fail: (err) => {
								console.log('失败了');
								console.log(err);
							}
						})
					},
					fail: (err) => {
						console.log('authorize=>fail:', fail);
					}
				}, )

				// #endif
			},
			// 计算是否可以使用金币抵扣
			getGoldNum() {
				let cost = 0;
				let gold = this.userInfo.gold_num
				let max = 0
				// 获取商品总价
				this.goods.forEach(item => {
					let price = Number(item[this.priceKey]) * Number(item.num)
					console.log(item.proportion,1234)
					max += Math.floor(price * item.proportion?.gold_proportion)
					this.totalPrice += price
				})
				// 排序商品
				let arr = this.goods.sort(this.compare)
				this.saveGoodsTemporary(arr)
				// let total = 0
				// arr.forEach(item => {
				// 	//original
				// 	total = Number(item.num * (item.price - item[this.priceKey]))
				// 	if (total <= gold) {
				// 		cost += total
				// 		gold -= total
				// 	}
				// })
				// 非升级产品才可以抵扣
				console.log(max);
				// if((!this.isUpdate) && this.userInfo.vip_level != 0){
				cost =  max <= this.userInfo.gold_num ? max : this.userInfo.gold_num
				if (!this.isUpdate) {
					if (cost > this.totalPrice) {
						cost = this.totalPrice / this.proportion
					}
					this.gold_num = cost > 0 ? cost : 0
				}
			},
			compare(x, y) {
				let n1 = Number(x.cost);
				let n2 = Number(y.cost);
				if (n1 > n2) {
					return 1;
				} else if (n1 < n2) {
					return -1
				} else {
					return 0
				}
			},
			choseAddress() {
				this.$Router.push(`/pages/common/address/address?openType=2`)
			},
			selectCoupon() {
				let that = this
				// 商品id
				var productIds = this.goods.map(item => item.id).join(",")
				uni.navigateTo({
					url: `/pages/mine/coupon/coupon?openType=2&productIds=${productIds}`,
					events: {
						acceptDataFromOpenedPage(data) {
							that.couponInfo = data
						}
					}
				})
			},
			// 不使用金币优惠
			surePay() {
				this.payState = true;
				this.remark = false
			},
			// 选择了支付方式
			payClick(index) {
				var that = this;
				if (this.vipBuy) {
					this.placeOrderOper(index).then(({
						status,
						id
					}) => {
						let orderId = id;
						console.log(orderId);
						if (status) {
							this.vipPrefix(orderId, index, that.goods[0].price).then(
								({
									status
								}) => {
									if (status) {
										uni.showToast({
											icon: "none",
											title: "会员升级成功",
											success: () => {
												setTimeout(() => {
													uni.redirectTo({
														url: "/pages/orderDetails/orderDetails?id=" +
															orderId,
													});
												}, 1500);
											},
										});
									} else {
										uni.showToast({
											icon: "none",
											title: "会员升级失败",
											success: () => {
												setTimeout(() => {
													uni.redirectTo({
														url: "/pages/orderDetails/orderDetails?id=" +
															orderId,
													});
												}, 1500);
											},
										});
									}
								}
							);
						} else {
							uni.redirectTo({
								url: "/pages/orderDetails/orderDetails?id=" + id,
							});
						}
					});
				} else {
					this.placeOrderOper(index).then(({
						status,
						id
					}) => {
						if (status) {
							uni.redirectTo({
								url: "/pages/orderDetails/orderDetails?id=" + id,
							});
						}
					});
				}
			},
			vipPrefix(orderId, state, price) {
				var that = this;
				return new Promise((resolve, reject) => {
					switch (Number(that.vipType)) {
						case 1:
							vipOpen({
								state: that.vipType,
								id: that.goods[0].price,
							}).then((resp) => {
								console.log("会员开通", resp);
								if (resp.code == 200) {
									this.setAttr({
										key: "vip",
										value: 1,
									}).then(() => {
										resolve({
											status: true
										});
									});
								} else {
									resolve({
										status: false
									});
								}
							});
							break;
						case 2:
							// this.cityOpen(this.payList[index].value);
							uni.redirectTo({
								url: "/pages/vip/subs/city/city?id=" +
									orderId +
									"&&state=" +
									state +
									"&&price=" +
									price,
							});
							break;
						case 3:
							vipStoreOpen({
								state: that.vipType,
								id: that.goods[0].price,
							}).then((resp) => {
								if (resp.code == 200) {
									this.setAttr({
										key: "landlord",
										value: 1,
									}).then(() => {
										resolve({
											status: true
										});
									});
								} else {
									resolve({
										status: false
									});
								}
							});
						default:
							return "";
					}
				});
			},
			// 城市股东开通
			cityOpen(state) {
				const {
					user,
					mobile,
					city
				} = this.city;
				vipCityOpen({
					user,
					mobile,
					id: city.value,
					price_id: this.price.id,
					state,
				}).then((resp) => {
					if (resp.code == 200) {
						if (resp.data.payRes) {
							// payment(resp.data.payRes).then((resp) => {});
							const {
								timeStamp,
								nonceStr,
								signType,
								paySign
							} = resp.data.payRes;
							uni.requestPayment({
								timeStamp,
								nonceStr,
								package: resp.data.payRes.package,
								signType,
								paySign,
								success: (resp) => {
									vipCitySucceed({
										user,
										mobile,
										id: city.value,
										price_id: this.price.id,
										state,
									}).then((resp) => {
										if (resp.code == 200) {
											uni.showToast({
												icon: "none",
												title: resp.msg,
											});
										}
									});
								},
								fail: (error) => {
									uni.showToast({
										icon: "none",
										title: "支付失败",
									});
								},
							});
						} else {
							uni.showToast({
								icon: "none",
								title: resp.msg,
							});
						}
					}
				});
			},
			// 获取当前的位置信息
			submitPrefix(callback) {
				return new Promise((resolve, reject) => {
					uni.authorize({
						scope: "scope.userLocation",
						success: () => {
							// callback();
							uni.getLocation({
								success: (resp) => {
									console.log('成功了');
									const {
										latitude,
										longitude
									} = resp;
									qqmapsdk.reverseGeocoder({
										location: {
											latitude,
											longitude,
										},
										success: (resp) => {
											const {
												province,
												city,
												district,
											} = resp.result.address_component;
											this.province = province;
											this.city = city;
											this.district = district;
											resolve();
										},
										fail: (error) => {
											uni.showToast({
												icon: "none",
												title: "地址解析失败，无法下单",
											});
											reject();
										},
									});
								},
								fail: (err) => {
									console.log("失败了", err);
								}
							});
						},
						fail: () => {
							this.showBtn = true;
							uni.showToast({
								icon: "none",
								title: "请开启小程序的位置授权",
								success: () => {
									uni.openSetting({
										success: (res) => {},
									});
									reject();
								},
							});
						},
					});
				});
			},
			openSetting(e) {
				const {
					authSetting
				} = e.detail;
				if (authSetting["scope.userLocation"]) {
					uni.showToast({
						icon: "none",
						title: "可确认购买！",
					});
				} else {
					uni.showToast({
						icon: "none",
						title: "未开启位置授权，无法下单",
					});
				}
			},
			// 下单
			placeOrderOper(index) {
				let goods_id = [],
					num = [],
					goods_stock_id = [],
					recommandId = [],
					cost = 0;
				this.goods.map((item) => {
					goods_id.push(item.id);
					num.push(item.num);
					goods_stock_id.push(item.stock_id);
					recommandId.push(item.recommandId);
					cost += item.num * item.cost;
				});
				let formData = {
					money: this.finalPrice,
					city_id: this.orderAddress.city_id,
					name: this.orderAddress.name,
					phone: this.orderAddress.mobile,
					address: this.orderAddress.city,
					address_detail: this.orderAddress.city_details,
					pay: this.payList[index].value, // 支付方式
					recommend_id: this.shareId || "", // 推荐人id
					goods_id: goods_id.join(","),
					num: num.join(","),
					goods_stock_id: goods_stock_id.join(","),
					order_province: this.province,
					order_city: this.city,
					order_district: this.district,
					cost: cost, // 成本价
					gold_num: this.gold_num,
					// 新增字段

					// 运费
					freight: this.addInfo.freight,
					// 抵扣积分
					integral: this.useIntegral ? 0 : this.userInfo.gold_num,
					// 优惠券id
					my_coupon_id: this.couponInfo ? this.couponInfo.id : 0,
					// 配送方式
					type: this.form.addressType
				};
				// 如果总价== 0，直接支付成功
				if (this.totalPrice == 0) {
					formData.pay = 3
				}
				if (!this.isUpdate) {
					return new Promise((resolve, reject) => {
						placeOrder(formData).then((resp) => {
							console.log("下单", resp);
							if (resp.code == 200) {
								var orderId = resp.data.id;
								if (resp.data.payRes) {
									const {
										timeStamp,
										nonceStr,
										signType,
										paySign,
									} = resp.data.payRes;
									uni.requestPayment({
										timeStamp,
										nonceStr,
										package: resp.data.payRes.package,
										signType,
										paySign,
										success: (resp) => {
											uni.showToast({
												icon: "none",
												title: "支付成功",
												success: () => {
													resolve({
														status: true,
														id: orderId
													});
												},
											});
										},
										fail: (error) => {
											console.log("payment失败", error);
											// orderId
											uni.showToast({
												icon: "none",
												title: "支付失败",
												success: () => {
													resolve({
														status: false,
														id: orderId
													});
													// resolve();
													// setTimeout(() => {
													//   uni.redirectTo({
													//     url: "/pages/orderDetails/orderDetails?id=" + orderId,
													//   });
													// }, 1200);
												},
											});
										},
									});
								} else {
									uni.showToast({
										icon: "none",
										title: resp.msg,
										success: () => {
											resolve({
												status: true,
												id: orderId
											});
										},
									});
								}
							}
							// else {
							//   uni.showToast({
							//     icon: "none",
							//     title: resp.msg,

							//   });
							// }
						});
					});
				} else {
					// 升级仅会有一个产品
					return new Promise((resolve, reject) => {
						memberUpdateApi({
							goods_id: goods_id,
							money: this.totalPrice
						}).then(res => {
							if (res.code === 0) {
								uni.showToast({
									icon: "none",
									title: res.msg
								})
								return
							}
							let data = res.data.payRes
							let orderId = res.data.orderno
							uni.requestPayment({
								...data,
								success: (resp) => {
									uni.showToast({
										icon: "none",
										title: "支付成功",
										success: () => {
											// resolve({ status: true, id: orderId });
											uni.switchTab({
												url: "../tabs/my/my"
											})
										},
									});
								},
								fail: (error) => {
									console.log("payment失败", error);
									// orderId
									uni.showToast({
										icon: "none",
										title: "支付失败",
										success: () => {
											// resolve({ status: false, id: orderId });
											// resolve();
											// setTimeout(() => {
											//   uni.redirectTo({
											//     url: "/pages/orderDetails/orderDetails?id=" + orderId,
											//   });
											// }, 1200);
										},
									});
								},
							})
						})
					})
				}
			},
		},
	};
</script>

<style lang="scss" scoped>
	@import './index.scss';
</style>
